define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluhouse/article/encyclopedia/index' + location.search,
                    add_url: 'xiluhouse/article/encyclopedia/add',
                    edit_url: 'xiluhouse/article/encyclopedia/edit',
                    del_url: 'xiluhouse/article/encyclopedia/del',
                    multi_url: 'xiluhouse/article/encyclopedia/multi',
                    articlecategory_url:'xiluhouse/article/category/index?classify=4',
                    dragsort_url:'',
                    table: 'article',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='article_category_id']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.articlecategory_url).data("params", '{"custom[classify]":"4"}').data("primaryKey", "id").data("field", "name");
            })
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),sortable:true},
                        {field: 'article_category_id', title: __('article_category_id'),formatter: function (value, row, index) {
                            if(row.article_category_id>0){
                                var html='';
                                var url =$.fn.bootstrapTable.defaults.extend.articlecategory_url+ "?id=" + row.article_category_id;
                                html+='<a  href="' + url + '" class="bg-info btn-dialog" title="查看关联信息"> '+row.articlecategory.name+' </a>';
                                return html;
                            }else{
                                return '-';
                            }
                        }
                        },
                        {field: 'name', title: __('Name'),operate:'LIKE',formatter: function (value, row, index) {
                            if(value==null||value==''){
                                return value;
                            }
                            if(value.length>21){
                                return value.substr(0, 21)+'...';
                            }else{
                                return value;
                            }
                        }},
                        {field: 'keywords', title: __('Keywords'),operate:'LIKE',visible:false},
                        {field: 'author', title: __('Author'),operate:'LIKE'},
                        {field: 'author_avatar_text', title: __('Author_avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'flag', title: __('Flag'), formatter: Table.api.formatter.flag},
                        {field: 'description', title: __('Description'),operate:'LIKE',visible:false},
                        {field: 'view_num', title: __('View_num'), operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'weigh', title: __('Weigh'),sortable:true},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Status normal'),"hidden":__('Status hidden')}, formatter: Table.api.formatter.status},
                        
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'xiluhouse/article/encyclopedia/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'xiluhouse/article/encyclopedia/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'xiluhouse/article/encyclopedia/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});